package com.uphone.kingmall.activity;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.LoginBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.main.MainActivity;
import com.uphone.kingmall.utils.ActivityCollector;
import com.uphone.kingmall.utils.DeviceUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PhoneFormatCheckUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentData;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/30
 *     desc   :
 * </pre>
 */
public class BindPhoneActivity extends BaseActivity {
    public static final int TYPE_WECHAT = 0X0011;
    public static final int TYPE_QQ = 0X0022;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_bind)
    Button btnBind;
    @IntentData
    private String openid = "";
    @IntentData
    private String nickname = "";
    @IntentData
    private String headimgurl = "";

    @IntentData
    private int type = TYPE_WECHAT;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_bind_phone;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_back, R.id.tv_get_yzm, R.id.btn_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                KeyboardUtils.hideSoftInput(this);
                break;
            case R.id.tv_get_yzm:
                getMachCode();
                break;
            case R.id.btn_bind:
                if (TextUtils.isEmpty(etPhone.getText().toString().trim()) || TextUtils.isEmpty(etCode.getText().toString().trim())) {
                    showShortToast("请将信息填写完整");
                    return;
                }
                if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
                    showShortToast("请输入正确的手机号");
                    return;
                }
                HttpParams params = new HttpParams();
                params.put("openid", openid);
                params.put("name", nickname);
                params.put("photo", headimgurl);
                params.put("mobile", etPhone.getText().toString().trim());
                params.put("code", etCode.getText().toString().trim());
                String url = MyUrl.wxloginBindTel;
                if (type == TYPE_QQ) {
                    url = MyUrl.qqloginBindTel;
                }
                OkGoUtils.progressRequest(url, this, params, new onNormalRequestListener() {
                    @Override
                    public void onSuccess(String response, int code) {
                        if (code == 0) {
                            LoginBean bean = GsonUtils.getGson().fromJson(response, LoginBean.class);
                            if (bean.getUser() != null) {
                                LoginBean.UserBean user = bean.getUser();
                                SharedPreferencesHelper.saveToken(user.getToken());
                                SharedPreferencesHelper.saveUserName(user.getUserName());
                                SharedPreferencesHelper.saveUserPhone(user.getUserMobile());
                                SharedPreferencesHelper.saveUserId(user.getUserId());
                                SharedPreferencesHelper.saveUserImg(user.getUserPhoto());
                                //设置极光推送别名
//                                JpushUtil.setTagAndAlias(BindPhoneActivity.this, user.getUserId());
//                                CommonUtil.loginRY();
                                KeyboardUtils.hideSoftInput(BindPhoneActivity.this);
                            }
                            if (bean.getCode() == 0) {
                                ToastUtils.showShortToast(BindPhoneActivity.this, "绑定成功！");
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        ActivityCollector.removeAll(MainActivity.class);
                                        finish();
                                    }
                                }, 500);
                            }
                        }
                        KeyboardUtils.hideSoftInput(BindPhoneActivity.this);
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
                break;
        }
    }

    private void getMachCode() {

        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            showShortToast("手机号不能为空");
            return;
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
            showShortToast("手机号不正确");
            return;
        }
        DeviceUtil.sendSmsCode(this, etPhone.getText().toString().trim(), "3", new DeviceUtil.DeviceCodeCallBack() {
            @Override
            public void callBack(String deviceCode) {
                timer.start();
                tvGetYzm.setEnabled(false);
                tvGetYzm.setTextColor(Color.parseColor("#cccccc"));
            }
        });

    }

    /**
     * 倒计时
     */
    CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            tvGetYzm.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetYzm.setEnabled(true);
            tvGetYzm.setText("重新获取");
            tvGetYzm.setTextColor(Color.parseColor("#E33344"));
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
